import { Component } from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams, ViewController} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {CustomerProvider} from "@providers/interface/customer";
import {DialogProvider} from "@providers/dialog";
import {AddressProvider} from "@providers/address";

/**
 * Generated class for the CustomerAddressAddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

declare let wechat;

@IonicPage()
@Component({
  selector: 'page-customer-address-add',
  templateUrl: 'customer-address-add.html',
})
export class CustomerAddressAddPage {

  isWechat=wechat;
  title:any;
  addressId: any;//选择的区域
  dependentColumns: any = [];//地址区域选项
  isname: string = '';
  ispho: string = '';
  ismor: boolean = false;
  userinfo: any;
  stuats = "0";
  dzmx: string;
  jiedao: string = "";
  jiedaolist: any[] = [];
  address: any = {name: "点击选择"};

  data: any = {};

  param: any = this.navParams.data;//上个传参

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private modalCtrl:ModalController,
              private viewCtrl: ViewController,
              private dialogProvider:DialogProvider,
              private storage:StorageProvider,
              private customerProvider:CustomerProvider,
              private addressService:AddressProvider,
              ) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CustomerAddressAddPage');
    this.userinfo = this.storage.read("userInfo");
    this.title = this.param.title

    if (this.param.ID) {
      this.isname = this.param.KHXM;
      this.ispho = this.param.LXDH;
      //this.bz = this.param.FLOOR;
      this.data.XXDZ = this.param.XXDZ;
      this.stuats=this.param.STUATS;
      //微信为下拉选择地址，不是微信的时候是点开地图选择
      if(wechat){
        this.data.ID = this.param.ID;
        if (this.param.DZ) {
          this.data.XXDQ = this.param.DZ;
          this.data.KHDQ = "";
          for (var i = 0; i < this.data.XXDQ.length; i++) {
            this.data.KHDQ += this.data.XXDQ[i].VALUE + " ";
          }
        }
      }else {
        if (this.param.LAT && this.param.LNG) {
          this.address = {
            "address": this.param.DZ[3]?this.param.DZ[3].VALUE:"",
            "addressComponent": {
              "city": this.param.DZ[0]?this.param.DZ[0].VALUE:"",
              "district": this.param.DZ[1]?this.param.DZ[1].VALUE:"",
              "province": this.param.DZ[2]?this.param.DZ[2].VALUE:"",
              "street": this.param.DZ[3]?this.param.DZ[3].VALUE:""
            },
            "location": {
              "lat": this.param.LAT,
              "lng": this.param.LNG
            },
            "name": this.param.ADDRESS
          }
        }
      }
      if (this.param.STUATS == 1) {
        this.ismor = true;
      }
    }

  }

  push(page: string) {
    if(wechat){
      this.addressAdd2();
    }else {
      this.addressAdd1();
    }
  }

  /**
   * 选择地区点击事件
   */
  chooseAddress() {
    if(wechat){
      this.addressService.selectAddress((addressList) => {
        this.data.KHDQ = "";
        this.data.XXDQ = addressList;
        for (var i = 0; i < addressList.length; i++) {
          this.data.KHDQ += addressList[i].VALUE + " ";
        }
      });
    }else{
      let value = null;
      if (this.address.location) {
        value = this.address;
      }
      let modal = this.modalCtrl.create('AddressMapPage', value);
      modal.onDidDismiss((data) => {
        if (data) {
          this.address = data;
          console.log(this.address);
        }
      });
      modal.present();
    }
  }

  /**
   * 将地址设置为默认
   */
  shimo() {
    if (this.ismor) {
      this.stuats = "1";
    } else {
      this.stuats = "0";
    }
  }

  addressAdd1(){
    let data = {
      "partyId": this.userinfo.PARTYID,
      "ADDRESS": this.address,
      "XXDZ": this.data.XXDZ || "",
      "stuats": this.stuats,
      "KHXM": this.isname,
      "LXDH": this.ispho
    };
    if (this.param.ID) {
      data['ID'] = this.param.ID;
    }

    this.customerProvider
      .addressAdd1(data)
      .then(()=>{
        this.viewCtrl.dismiss();
      })
      .catch((error)=>{
        this.dialogProvider.showAlert('新增地址错误：' + String(error));
      });
  }

  addressAdd2(){
    this.data["partyId"] = this.userinfo.PARTYID;
    if(this.data.XXDQ){
      this.data.DQXX01 = this.data.XXDQ[this.data.XXDQ.length - 1]["KEY"];
      this.data.XXDQ.push({"VALUE": this.data.XXDZ || ''});
    }
    this.data.XXDZ = this.data.XXDZ || "";
    this.data.LXDH = this.ispho;
    this.data.KHXM = this.isname;
    this.data.stuats=this.stuats;
    this.customerProvider
      .addressAdd2(this.data)
      .then(()=>{
        this.viewCtrl.dismiss();
      })
      .catch(()=>{
      });
  }

}
